package com.yuandianrrp;

import com.fazecast.jSerialComm.SerialPort;
import com.yuandianrrp.wms.SwingUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author Administrator
 */
public class SerialPortTestUtil extends JFrame {
    private static final long serialVersionUID = 6564112175319477590L;
    private static com.fazecast.jSerialComm.SerialPort serialPort2 = null;
    private boolean isSend = false;

    public SerialPortTestUtil() {
        setTitle("串口调试");
        setIconImage(new ImageIcon(getClass().getResource("/images/ph.png")).getImage());
        setBounds(SwingUtils.center(false, null, 430, 300));
        setResizable(false);
        JButton jButton1 = new JButton("发送串口数据");
        jButton1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                SerialPortTestUtil.this.isSend = true;
            }
        });
        JButton jButton2 = new JButton("停止发送数据");
        jButton2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                SerialPortTestUtil.this.isSend = false;
            }
        });
        JPanel jPanel = new JPanel(new FlowLayout(1));
        jPanel.add(jButton1);
        jPanel.add(jButton2);
        setLayout(new BorderLayout());
        add(jPanel, "Center");
        serialPortInit();
        sendSerialPortData();
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                if (serialPort2 != null) {
                    serialPort2.closePort();
                }
                System.exit(0);
            }
        });
        setVisible(true);
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                if (serialPort2 != null) {
                    serialPort2.closePort();
                }
            }
        });
    }

    public static void main(String[] args) {
        new SerialPortTestUtil();
    }

    private void serialPortInit() {
        SerialPort[] commPorts = SerialPort.getCommPorts();
        for (SerialPort commPort : commPorts) {
            System.out.println(" >>>>>> " + commPort.getSystemPortName());
        }
        serialPort2 = SerialPort.getCommPort("COM4");
        serialPort2.openPort();
    }


    private void sendSerialPortData() {
        new Thread(new SerialPortSendRunnable()).start();
    }

    public class SerialPortSendRunnable implements Runnable {
        private SerialPortSendRunnable() {
        }

        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                    if (SerialPortTestUtil.this.isSend) {
                        String random = String.valueOf((int) ((Math.random() * 15.0D + 4.0D) * 10.0D) * 10);
                        System.out.println("发送数据:" + random);
                        byte[] bytes = String.valueOf(random).getBytes();
                        serialPort2.writeBytes(bytes, bytes.length);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
